﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace UIT_CV_Management
{
    public class NgheNghiep
    {
        public String maNgheNghiep;
        private String donViCongTac;
        private String chucVu;
        private String chuyenMon;
        private float luong;
        private Database myDatabase = Database.shareDatabase;

        public NgheNghiep(String _donViCongTac, String _chucVu, String _chuyenMon, float _luong)
        {
            maNgheNghiep = Database.getID("NgheNghiep", "MaNgheNghiep");

            donViCongTac = _donViCongTac;
            chucVu = _chucVu;
            chuyenMon = _chuyenMon;
            luong = _luong;
        }
        public NgheNghiep(String _maNgheNghiep)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM NgheNghiep WHERE MaNgheNghiep = '" + _maNgheNghiep + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                maNgheNghiep = table.Rows[0]["MaNgheNghiep"].ToString();
                donViCongTac = table.Rows[0]["DonViCongTac"].ToString();
                chucVu = table.Rows[0]["ChucVu"].ToString();
                chuyenMon = table.Rows[0]["ChuyenMon"].ToString();
                luong = float.Parse(table.Rows[0]["Luong"].ToString());
            }
        }
        public Boolean Save()
        {
            String sqlNgheNghiep = "INSERT INTO NgheNghiep VALUES('" 
                + maNgheNghiep + "',N'" 
                + donViCongTac + "',N'" 
                + chucVu + "'," 
                + luong + ",N'" 
                + chuyenMon + "')";
            QueryResult result = myDatabase.Query(sqlNgheNghiep);

            if (result.ReturnCode == 409)
            {
                return false;
            }
            return true;
        }
        public Boolean Update()
        {
            bool isDupplicated = Database.isDupplicate("NgheNghiep", "MaNgheNghiep", maNgheNghiep);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE NgheNghiep SET " 
                    + "DonViCongTac = N'" + donViCongTac 
                    + "', ChucVu = N'" + chucVu 
                    + "',Luong = " + luong 
                    + ",ChuyenMon = N'" + chuyenMon + "'"
                    + " WHERE MaNgheNghiep = '" + maNgheNghiep + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    return false;

            }
            return true;
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM NgheNghiep WHERE MaNgheNghiep = '" + maNgheNghiep + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            return true;
        }
    }
}
